
#include	 "Bus.h"
#include    "ConfigIO.H"

/*测试*/
#include "unity.h"

 

unsigned char    ReadDinBusDin1(void);
unsigned char    ReadDinBusDin2(void);
void WriteDoutBusDout1(unsigned char data);
void WriteDoutBusDout2(unsigned char data);


BusOut_Type	busOutCtlIO;//输出使能控制总线类型
BusIn_Type	busInCtlIO; //输入使能控制总线类型
DinDataBit16_Type	dinDataRead ;
DoutDataBit16_Type	doutDataWrite;


DeactActMode_Type   In_EnIfValidStr;//Din1_en Din2_en  Data_en(Din0_en)      输入使能信号的有效和无效电平结构体
DeactActMode_Type   Out_EnIfValidStr;//Dout1_en Dout2_en  port_en(Dout0_en)  输出使能信号的有效和无效电平结构体

LevelStateEnum   DinValidLevel;			//Din1   Din2的输入信号有效电平
LevelStateEnum   DoutValidLevel;		//Dout1 Dout2的输出信号有效电平
LevelStateEnum   DatainValidLevel;		//Data 数据采集的信号有效电平
//LevelStateEnum   PortoutValidLevel;	//Port 输出的信号有效电平  在end.c中控制


//总线配置:给全局变量进行配置，每次先读配置
//使能信号需要配置有效电平和无效电平
//输入输出信号只需要配置有效电平
void	BusConfig(void){
	
	//输入使能信号的有效电平=0
	In_EnIfValidStr.ValidLevel=LevelLow;
	//输入使能信号的无效电平=1
	In_EnIfValidStr.InValidLevel=LevelHigh;
	
	//输 出使能信号的有效电平=1
	Out_EnIfValidStr.ValidLevel=LevelHigh;
	//输出使能信号的无效电平=0
	Out_EnIfValidStr.InValidLevel=LevelLow;
	

	//数据采集的信号有效电平=1
	DatainValidLevel=LevelHigh;
	//输入信号的有效电平=0
	DinValidLevel=LevelLow;	
	
	//输出信号有效电平=0
	DoutValidLevel=LevelLow;
	//子卡端选输出信号的有效电平=0
	//PortoutValidLevel=LevelLow;
} 

/**
 * @brief 读 输入数据
 * 从8路数据输入引脚读取数据，并将这些数据组合成一个 unsigned char 类型的值
 * @return unsigned char
 */
unsigned char ReadBus(void)
{
	unsigned char data = 0;
	data = (unsigned char)(Mcu_Data_DIN0 | 
			Mcu_Data_DIN1 << 1 |
			Mcu_Data_DIN2 << 2 |
			Mcu_Data_DIN3 << 3 |
			Mcu_Data_DIN4 << 4 |
			Mcu_Data_DIN5 << 5 |
			Mcu_Data_DIN6 << 6 |
			Mcu_Data_DIN7 << 7 );
		
	return data;
	
}


/**
 * @brief 写数据
 * 将一个 unsigned char 类型的数据写入多个输出引脚
 * @param data ? 这里的data直接是某个引脚的高低电平
 */
void WriteBus(unsigned char data)
{
	MCU_CMD_DOUT0 = data & 0X01;
    MCU_CMD_DOUT1 = (data & 0x02) >> 1 ;
	MCU_CMD_DOUT2 = (data & 0x04) >> 2 ;
	MCU_CMD_DOUT3 = (data & 0x08) >> 3 ;
    MCU_CMD_DOUT4 = (data & 0x10) >> 4 ;
    MCU_CMD_DOUT5 = (data & 0x20) >> 5 ;
    MCU_CMD_DOUT6 = (data & 0x40) >> 6 ;
	MCU_CMD_DOUT7 = (data & 0x80) >> 7 ;
	
}
/***MCU_CMD_DOUT7*********************控制信号操作************************************/

/**3组输入总线复用控制引脚******
MCU_DATA_EN
MCU_IN1_EN
MCU_IN2_EN
**0：使能数据，1无效*/

void	DataBusEnable(void) {
	MCU_DATA_EN = busInCtlIO.DataEnIO = In_EnIfValidStr.ValidLevel;//有效
}

//数据采集总线禁用
void	DataBusDisable(void)
{
		MCU_DATA_EN	=	busInCtlIO.DataEnIO	= In_EnIfValidStr.InValidLevel;
}

//Din0-7总线使能
void	Din1BusEnable(void){								
		MCU_IN1_EN	=	busInCtlIO.Din1EnIO	=	In_EnIfValidStr.ValidLevel;	
}

// Din0-7总线禁用
void	Din1BusDisable(void){
		MCU_IN1_EN	=	busInCtlIO.Din1EnIO	=	In_EnIfValidStr.InValidLevel;
}
// Din8-15总线使能
void	Din2BusEnable(void){								
		MCU_IN2_EN	=	busInCtlIO.Din1EnIO	=	In_EnIfValidStr.ValidLevel;	
}
//Din8-15总线禁用
void	Din2BusDisable(void){
		MCU_IN2_EN	=	busInCtlIO.Din1EnIO	=	In_EnIfValidStr.InValidLevel;
}
/**3组输出总线复用控制引脚**********
MCU_CMD_EN
MCU_OUT1_EN
MCU_OUT2_EN
***0：无效，1使能数据**********/

void	CommandBusEnable(void){							
		MCU_CMD_EN	=	busOutCtlIO.CommandEnIO	=Out_EnIfValidStr.ValidLevel;	
}
void	CommandBusDisable(void){
		MCU_CMD_EN	=	busOutCtlIO.CommandEnIO	=Out_EnIfValidStr.InValidLevel;
}
void	Dout1BusEnable(void){								
		MCU_OUT1_EN	=	busOutCtlIO.Dout1EnIO	=Out_EnIfValidStr.ValidLevel;	 
}
void	Dout1BusDisable(void){
		MCU_OUT1_EN	=	busOutCtlIO.Dout1EnIO	=Out_EnIfValidStr.InValidLevel;
}
void	Dout2BusEnable(void){								
		MCU_OUT2_EN	=	busOutCtlIO.Dout2EnIO	=Out_EnIfValidStr.ValidLevel;	 
}
void	Dout2BusDisable(void){
		MCU_OUT2_EN	=	busOutCtlIO.Dout2EnIO	=Out_EnIfValidStr.InValidLevel;
}

/***************总线使能三选一************************/
// 数据采集总线使能
void DataBusOpen()
{
	DataBusEnable();
	Din1BusDisable();
	Din2BusDisable();
}

//信号采集总线关闭
void DataBusClose(void)
{
	DataBusDisable();
}

//8路输入总线打开Dout0-7
void	Din1BusOpen(void){
		
		DataBusDisable();
		Din1BusEnable();
		Din2BusDisable();
}

//8路输入总线关闭
void	Din1BusClose(void){
		Din1BusDisable()	;
}

//8路输出Dout0-7打开
void	Dout1BusOpen(void){
		CommandBusDisable();
		Dout1BusEnable();
		Dout2BusDisable();
}
//8路输出Dout0-7关闭
void	Dout1BusClose(void){
		Dout1BusDisable()	;
}
//Dout8-15输出总线打开
void	Dout2BusOpen(void){
		CommandBusDisable();
		Dout1BusDisable();
		Dout2BusEnable();
}
//Dout8-15输出总线打开
void	Dout2BusClose(void){
		Dout2BusDisable()	;
}

/***************************对外api****************************************/

//读信号采集数据，返回8位数据，有数据位为1
unsigned char  ReadDataBusData(void)
{
	
	//使能信号采集总线
	DataBusOpen();
	
	//读八个引脚数据,数据采集有效
	unsigned char data = ReadBus();
	
	//如果引脚是低电平有效，需要翻转，表述有效数据位为1
	if ( DatainValidLevel == 0) {
		data = ~data;
	}

	//关闭信号采集总线使能
	DataBusClose();
	
	return data ;
	
}


//读取外部输入DIN0-7（中间函数）
unsigned char  ReadDinBusDin1(void)
{
	Din1BusOpen();
	DinDataBit16_Type ReadData;
	ReadData.u8.din1 = ReadBus();
	
	
	Din1BusClose();
	
	//这里有效电平可能是低电平
	return ReadData.u8.din1 ;
 }


//读取外部输入DIN8-15
unsigned char  ReadDinBusDin2(void)
{
	Din1BusOpen();
	DinDataBit16_Type ReadData;
	ReadData.u8.din2 = ReadBus();
	
	
	
	Din1BusClose();
	

	return ReadData.u8.din2 ;
 }

//读取全部外部输入 16位（对外api)
unsigned short  ReadDinBus(void)
{
    DinDataBit16_Type dataDin;
	
    dataDin.u8.din1 =  ReadDinBusDin1();//先打开din1读完关闭
    dataDin.u8.din2 =  ReadDinBusDin2();//再打开din2读完关闭
	if(DinValidLevel == 0){
		dataDin.u16=~(dataDin.u16);
	}
	
    return  dataDin.u16;
}


//写外部输出Dout0-7（将data写入输出引脚寄存器）
void    WriteDoutBusDout1(unsigned char data){
	
	//8路输出Dout0-7打开
	Dout1BusOpen();
	//将一个 data 写入多个输出引脚
	WriteBus(data);
	//8路输出Dout0-7关闭
	Dout1BusClose();
}

//写外部输出Dout8-15
void   WriteDoutBusDout2(unsigned char data){
	
	Dout2BusOpen();
	WriteBus(data);
	Dout2BusClose();
      
}

//写外部输出,将16位data写入8路输出引脚寄存器
void   WriteDoutBus(unsigned short data)
{
	
	
	
	
	
	
}


//写端选信号？？












